package com.mav.aop;

import com.mav.mapper.LoginLogMapper;
import com.mav.mapper.OperateLogMapper;
import com.mav.pojo.Emp;
import com.mav.pojo.EmpLoginLog;
import com.mav.pojo.LoginInfo;
import com.mav.pojo.OperateLog;
import com.mav.util.CurrentHolder;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Currency;

@Slf4j
@AllArgsConstructor
@Aspect
@Component
public class OperationLogAspect {
    @Autowired
    OperateLogMapper operateLogMapper;
    @Autowired
    LoginLogMapper loginLogMapper;

    @Around("execution(* com.mav.service.iml.*.*(..))")
    public Object logOperation(ProceedingJoinPoint joinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long endTime = System.currentTimeMillis();
        long costTime = endTime - startTime;
        OperateLog operateLog = new OperateLog();
        operateLog.setOperateEmpId(CurrentHolder.getCurrentId());
        operateLog.setOperateTime(LocalDateTime.now());
        operateLog.setClassName(joinPoint.getClass().getName());
        String methodName = joinPoint.getSignature().getName();
        operateLog.setMethodName(methodName);
        operateLog.setMethodParams("666");
        operateLog.setReturnValue("666");
        operateLog.setCostTime(costTime);
        operateLogMapper.insert(operateLog);
        if (methodName.equals("getLoginInfo")) {
            Short success=1;
            Short defeat=0;
            if (result != null) {
                LoginInfo loginInfo = (LoginInfo) result;
                EmpLoginLog empLoginLog=new EmpLoginLog();
                empLoginLog.setUsername(loginInfo.getUsername());
                empLoginLog.setPassword(loginInfo.getPassword());
                empLoginLog.setLoginTime(LocalDateTime.now());
                empLoginLog.setIsSuccess(success);
                empLoginLog.setJwt(loginInfo.getToken());
                empLoginLog.setCostTime(costTime);
                loginLogMapper.insert(empLoginLog);
            }
            else {
                Object[] args = joinPoint.getArgs();
                Emp arg = (Emp) args[0];
                EmpLoginLog empLoginLog=new EmpLoginLog();
                empLoginLog.setUsername(arg.getUsername());
                empLoginLog.setPassword(arg.getPassword());
                empLoginLog.setIsSuccess(defeat);
                empLoginLog.setCostTime(costTime);
                loginLogMapper.insert(empLoginLog);
            }
        }
        log.info("操作日志信息:{}", operateLog);
        return result;
    }

}
